home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 72 / IOPROG_72.ISO / soft / Codice / Web Application in Java / eLisa / Java Web App / support / src / Prodotti.java < prev    next >
Encoding:
Java Source  |  2003-07-02  |  6.5 KB  |  196 lines

  1. import java.io.*;
  2. import java.sql.*;
  3. import java.util.*;
  4.  
  5. import javax.xml.parsers.*;
  6.  
  7. import org.w3c.dom.*;
  8. import org.xml.sax.*;
  9.  
  10. public class Prodotti extends TabellaDB {
  11.     
  12.     public Prodotti( Connection conn ) {
  13.         super(conn);
  14.     }
  15.     
  16.     void drop() throws SQLException {
  17.         String sql = "DROP TABLE PRODOTTI";
  18.         execute( sql );
  19.     }
  20.     
  21.     void create() throws SQLException {
  22.         String sql = "CREATE TABLE PRODOTTI ("+
  23.                           "ID int(11) NOT NULL auto_increment,"+
  24.                           "NOME varchar(100) default '',"+
  25.                           "DESCRIZIONE text,"+
  26.                           "PREZZO float NOT NULL default '0',"+
  27.                           "SCONTO float NOT NULL default '0',"+
  28.                           "CODICE_IVA int(11) NOT NULL default '0',"+
  29.                           "CATEGORIA int(11) NOT NULL default '0',"+
  30.                           "IMMAGINE blob,"+
  31.                           "LINK varchar(255),"+
  32.                           "PRIMARY KEY  (id),"+
  33.                           "KEY prodotti_idx1 (CATEGORIA,ID),"+
  34.                           "KEY prodotti_idx2 (CATEGORIA)"+
  35.                       ")";
  36.         execute( sql );
  37.     }
  38.     
  39.     void fill() throws SQLException, Exception {
  40.         String sql = "INSERT INTO PRODOTTI (NOME, DESCRIZIONE, PREZZO, SCONTO, CODICE_IVA,"+
  41.             "CATEGORIA, IMMAGINE, LINK) VALUES (?,?,?,?,?,?,?,?)";
  42.             
  43.         PreparedStatement pstmt = conn.prepareStatement( sql );
  44.         
  45.         DatiProdotto[] dp = loadDatiProdotti();
  46.         
  47.         for( int i=0; i<dp.length; i++ ) {
  48.             int pos = 1;
  49.             
  50.             pstmt.setString( pos++, dp[i].nome );
  51.             pstmt.setString( pos++, dp[i].descrizione );
  52.             pstmt.setFloat( pos++, dp[i].prezzo );
  53.             pstmt.setFloat( pos++, dp[i].sconto );
  54.             pstmt.setInt( pos++, dp[i].codiceIva );
  55.             pstmt.setInt( pos++, dp[i].categoria );
  56.             pstmt.setBytes( pos++, dp[i].immagine );
  57.             pstmt.setString( pos++, dp[i].link );
  58.             
  59.             log( "Prodotti: "+dp[i].nome+", "+dp[i].prezzo );
  60.             
  61.             pstmt.executeUpdate();
  62.         }
  63.         pstmt.close();
  64.             
  65.     }
  66.     
  67.     DatiProdotto[] loadDatiProdotti() throws ParserConfigurationException, SAXException, IOException {
  68.         List elenco = new LinkedList();
  69.         String sourceFile = "prodotti.xml";
  70.         
  71.         DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
  72.         
  73.         log("Prodotti: utilizzo il file "+ sourceFile );
  74.         
  75.         File inputFile = new File( sourceFile );
  76.         Document doc = db.parse( inputFile );
  77.         NodeList nl = doc.getElementsByTagName( "prodotto" );
  78.         
  79.         for( int i=0; i<nl.getLength(); i++ ) {
  80.             Node node = nl.item(i);
  81.             DatiProdotto dp = new DatiProdotto();
  82.             
  83.             NamedNodeMap attributi = node.getAttributes();
  84.             dp.nome = attributi.getNamedItem( "nome" ).getNodeValue();
  85.             
  86.             caricaDatiProdotto( node.getChildNodes(), dp );
  87.             System.out.println( dp );
  88.             
  89.             elenco.add( dp );
  90.         }
  91.         
  92.         return (DatiProdotto [])elenco.toArray( new DatiProdotto[ elenco.size() ] );
  93.     }
  94.     
  95.     void caricaDatiProdotto( NodeList lista, DatiProdotto dp ) throws IOException {
  96.         for( int i=0; i<lista.getLength(); i++ ) {
  97.             Node node = lista.item(i);
  98.             String name = node.getNodeName();
  99.             
  100.             if( name.equals("descrizione") ) {
  101.                 dp.descrizione = getTextChild( node );
  102.             }
  103.             if( name.equals("prezzo") ) {
  104.                 dp.prezzo = parseFloat( getTextChild( node ) );
  105.             }
  106.             if( name.equals("sconto") ) {
  107.                 dp.sconto = parseFloat( getTextChild( node ) );
  108.             }
  109.             if( name.equals("codiceIva") ) {
  110.                 dp.codiceIva = parseInt( getTextChild( node ) );
  111.             }
  112.             if( name.equals("categoria") ) {
  113.                 dp.categoria = parseInt( getTextChild( node ) );
  114.             }
  115.             if( name.equals("link") ) {
  116.                 dp.link = getTextChild( node );
  117.             }
  118.             if( name.equals("immagine") ) {
  119.                 dp.immagine = loadImage( getTextChild( node ) );
  120.             }
  121.         }
  122.     }
  123.     
  124.     String getTextChild( Node node ) {
  125.         NodeList list = node.getChildNodes();
  126.         for( int i=0; i<list.getLength(); i++ ) {
  127.             Node item = list.item(i);
  128.             
  129.             if( item.getNodeType() == Node.TEXT_NODE ) {
  130.                 return item.getNodeValue();
  131.             }
  132.         }
  133.         
  134.         return null;
  135.     }
  136.     
  137.     float parseFloat( String value ) {
  138.         float result = 0;
  139.         try {
  140.             result = Float.parseFloat( value );
  141.         } catch( NumberFormatException ex ) {
  142.         }
  143.         return result;
  144.     }
  145.     
  146.     int parseInt( String value ) {
  147.         int result = 0;
  148.         try {
  149.             result = Integer.parseInt( value );
  150.         } catch( NumberFormatException ex ) {
  151.         }
  152.         return result;
  153.     }
  154.     
  155.     static final int BUF_SIZE = 4096;
  156.     
  157.     byte[] loadImage( String name ) throws IOException {
  158.         String filename = "immagini"+File.separator+name;
  159.         System.out.println( filename );
  160.         
  161.         FileInputStream fis = new FileInputStream(filename);
  162.         ByteArrayOutputStream ba = new ByteArrayOutputStream();
  163.         byte[] buffer = new byte[BUF_SIZE];
  164.         int len;
  165.         
  166.         while( true ) {
  167.             len = fis.read( buffer );
  168.             if( len > 0 ) {
  169.                 ba.write( buffer, 0, len );
  170.             } else {
  171.                 break;
  172.             }
  173.         }
  174.         
  175.         return ba.toByteArray();
  176.     }
  177.     
  178.     class DatiProdotto {
  179.         public String nome;
  180.         public String descrizione;
  181.         public float prezzo;
  182.         public float sconto;
  183.         public int codiceIva;
  184.         public int categoria;
  185.         byte[] immagine;
  186.         public String link;
  187.         
  188.         public String toString() {
  189.             String result = "DatiProdotto=[nome="+nome+",\ndescrizione="+descrizione+
  190.                 ",\nprezzo="+prezzo+",\n sconto="+sconto+",\n codiceIva="+codiceIva+
  191.                 ",\ncategoria="+categoria+",\nimmagine="+immagine+",\n link="+link;
  192.             return result;
  193.         }
  194.     }
  195. }
  196.